package com.htgraph.renderer;

import com.ht.aems.util.RtDataBuffer;
import com.ht.impl.ResourceTypeCons;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.model.mxIGraphModel;
import org.w3c.dom.Element;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.text.DecimalFormat;


/**
 * Created by IntelliJ IDEA.
 *
 * @author Wang Jiaxiang
 *         Date: 2010-7-2
 */
public class CellRenderer4Old extends DefaultCellRenderer implements ResourceTypeCons {

    public static final String DATASOURCE_SE = "SE";
    public static final String DATASOURCE_SCADA = "SCADA";
    public static final String DATASOURCE_PMU = "PMU";
    public static final String POWERTYPE_ACTIVE = "p_value";
    public static final String POWERTYPE_REACTIVE = "q_value";

    private String dataSource = DATASOURCE_SCADA;
    private String powerType = POWERTYPE_ACTIVE;
    Map<String, Boolean> stateBuffer = new HashMap<String, Boolean>();

    public CellRenderer4Old() {
    }

    public CellRenderer4Old(mxIGraphModel model) {
        super(model);
    }
    DecimalFormat format = new DecimalFormat("#.##");
    public void updateCells(RtDataBuffer buffer) {
        Map<String, Object> cells = ((mxGraphModel) model).getCells();
        flasher.clear();
        model.beginUpdate();
        for (Object obj : cells.keySet()) {
            mxCell cell = (mxCell) cells.get(obj);
            Object value = cell.getValue();
            if (!(value instanceof Element))
                continue;
            Element e = (Element) value;
            Object memValue;
            String measKey;
            if(e.getNodeName().equals("label")) {
                try {
                    measKey = e.getAttribute("measKey");
                    if(!dataSource.equals(DATASOURCE_SCADA))
                         measKey = measKey.replaceAll(DATASOURCE_SCADA, dataSource);
                    if(!measKey.contains(powerType))
                        measKey = measKey.replaceAll(POWERTYPE_ACTIVE, powerType);
                    memValue = buffer.get(measKey);
                    if(memValue == null){
                        e.setTextContent("");
                    }else {
                        e.setTextContent(format.format(memValue));
                    }
                } catch (Exception e1) {
                    continue;
                }
            }
            model.setValue(cell, e);
        }
        model.endUpdate();
    }
    
    public List<String> buildKeys() {
        return buildKeys(new String[]{DATASOURCE_PMU, DATASOURCE_SE});
    }

    public List<String> buildKeys(String []types) {
        Map<String, Object> cells = ((mxGraphModel) model).getCells();
        List<String> keys = new ArrayList<String>();
        for (Object obj : cells.keySet()) {
            mxCell cell = (mxCell) cells.get(obj);
            Object value = cell.getValue();
            if (!(value instanceof Element))
                continue;
            Element e = (Element) value;
            String id_scada = e.getAttribute("measKey");
            if (id_scada == null || id_scada.equals(""))
                continue;
            keys.add(id_scada);
            if(id_scada.contains(POWERTYPE_ACTIVE))
                keys.add(id_scada.replaceAll(POWERTYPE_ACTIVE, POWERTYPE_REACTIVE));
            for(String type : types){
                String id = id_scada.replaceAll(DATASOURCE_SCADA, type);
                keys.add(id);
                if(id.contains(POWERTYPE_ACTIVE))
                keys.add(id.replaceAll(POWERTYPE_ACTIVE, POWERTYPE_REACTIVE));
            }
        }
        return keys;
    }


    public void setDataSource(String dataSource) {
        this.dataSource = dataSource;
    }

    public String getPowerType() {
        return powerType;
    }

    public void setPowerType(String powerType) {
        this.powerType = powerType;
    }
}